package org.itboys.mobile.controller.weixin.address;

import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
import org.itboys.commons.CommonConstants;
import org.itboys.commons.dto.ResultPageDOHelper;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mobile.entity.mongo.SundayAddress;
import org.itboys.mobile.entity.mongo.SundayMember;
import org.itboys.mobile.util.HttpRequestUtil;
import org.itboys.mobile.util.ResponseMessage;
import org.itboys.mobile.service.mongo.weixin.address.SundayWeixinAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created on 2017/11/29.
 * @author 朱坤涛
 */
@Controller
@RequestMapping("/sunday/weixin/address")
public class SundayWeixinAddressController extends BaseController{
    @Autowired
    private SundayWeixinAddressService addressService;

    /**
     * 查询用户所有收货地址
     * @param request
     * @param response
     * @param

     */
    @RequestMapping(value="/select", method= RequestMethod.POST)
    public void select(
            HttpServletRequest request,
            HttpServletResponse response){
        try {
            SundayMember sundayMember = new SundayMember();
            Long memberId = sundayMember.getId();
            List<SundayAddress> result =addressService.selectAddress(memberId);
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgResultDO(result, CommonConstants.SUCCESS));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * 保存或者修改收货地址
     * @param request
     * @param response
     * @param id
     * @param
     * @param receiveName
     * @param receiveMobile
     * @param receiveAddress
     */
    @RequestMapping(value="/save", method= RequestMethod.POST)
    public void save(HttpServletRequest request,
                     HttpServletResponse response,
                     @ApiParam(required=true, name="id", value="地址id。新增时传入0")@RequestParam(value = "id", required = false)Long id,
                     @ApiParam(required=true, name="receiveName", value="收货人姓名")@RequestParam(value = "receiveName", required = true)String receiveName,
                     @ApiParam(required=true, name="receiveMobile", value="手机号码")@RequestParam(value = "receiveMobile", required = true)String receiveMobile,
                     @ApiParam(required=false, name="provinceId", value="省ID。管理版传入")@RequestParam(value = "provinceId", required = false)Long provinceId,
                     @ApiParam(required=false, name="provinceName", value="省名称。管理版传入")@RequestParam(value = "provinceName", required = false)String provinceName,
                     @ApiParam(required=false, name="cityId", value="市ID。管理版传入")@RequestParam(value = "cityId", required = false)Long cityId,
                     @ApiParam(required=false, name="cityName", value="市名称。管理版传入")@RequestParam(value = "cityName", required = false)String cityName,
                     @ApiParam(required=false, name="districtId", value="区县ID。管理版传入")@RequestParam(value = "districtId", required = false)Long districtId,
                     @ApiParam(required=false, name="districtName", value="区县名称。管理版传入")@RequestParam(value = "districtName", required = false)String districtName,
                     @ApiParam(required=true, name="receiveAddress", value="详细地址")@RequestParam(value = "receiveAddress", required = true)String receiveAddress
    ){
        try{
            Long memberId = HttpRequestUtil.getUser(request).getId();
            addressService.saveAddress(id,memberId,receiveName,receiveMobile,provinceId,provinceName,cityId,cityName,districtId,districtName,receiveAddress);
            ResponseMessage.success();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 设置默认地址
     * @param request
     * @param response
     * @param
     * @param addressId
     */
    @RequestMapping(value="/setDefault", method=RequestMethod.POST)
    public void setDefault(
            HttpServletRequest request,
            HttpServletResponse response,
            @ApiParam(required=true, name="addressId", value="地址id")@RequestParam(value = "addressId", required = true)Long addressId){
        try{
            Map<String, Object> param =new HashMap<String,Object>();
            Long memberId = HttpRequestUtil.getUser(request).getId();
            addressService.setDefaultAddress(addressId, memberId);
            ResponseMessage.success();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 单个删除收货地址
     * @param request
     * @param response
     * @param addressId
     */
    @RequestMapping(value="/delete", method=RequestMethod.POST)
    @ApiOperation(value="【用户版和管理版】删除地址")
    public void delete(
            HttpServletRequest request,
            HttpServletResponse response,
            @ApiParam(required=true, name="addressId", value="地址id")@RequestParam(value = "addressId", required = true)Long addressId){
        try{
            Map<String, Object> param =new HashMap<String,Object>();
            param.put("addressId",addressId);
            addressService.delete(addressId);
            ResponseMessage.success();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

}
